package com.bss.service.impl;

import com.bss.dao.OrderMapper;
import com.bss.pojo.Order;
import com.bss.pojo.OrderExample;
import com.bss.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author 袁飞
 * @company 中原工学院
 * @create 2020-03-14-19:35
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;
    public int addOrder(Order Order) {
        return orderMapper.insert(Order);
    }

    public int updateOrder(Order Order) {
        return orderMapper.updateByPrimaryKeySelective(Order);
    }

    /**
     * 通过orderID，而不是ID查找订单
     * @param Id
     * @return
     */
    public Order findOrderById(Integer Id) {
        return orderMapper.selectByPrimaryKey(Id);
    }

    public int deleteOrderById(Integer Id) {
        return orderMapper.deleteByPrimaryKey(Id);
    }

    public List<Order> findAll() {
        return orderMapper.selectByExample(null);
    }

    /**
     * 取消订单
     * @param Id
     * @return
     */
    public int cancelOrder(Integer Id) {
        Order order = orderMapper.selectByPrimaryKey(Id);
        order.setOrderStatus(1);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    public List<Order> findOrder(String orderId, String businessName, BigDecimal max, BigDecimal min) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andOrderIdLike("%"+orderId+"%");
        criteria.andBusinessNameLike("%"+businessName+"%");
        criteria.andTotalPriceGreaterThanOrEqualTo(min);
        criteria.andTotalPriceLessThanOrEqualTo(max);
        return orderMapper.selectByExample(orderExample);
    }

}
